<?php
require_once '../DTO/binhluan.php';
require_once 'DBConnection.php';

class binhluanDAO extends DBConnection{
	public function __construct(){
		parent::__construct();
	}
	
	function extractBinhLuan($sql){
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("failed getting binh luan");
		}
		
		if(mysql_num_rows($res)){
			$obj = array();
			$temp = mysql_fetch_assoc($res);
			while ($temp){
				$obj[] = new binhluan($temp['mabinhluan'], $temp['macauhoi'], $temp['taikhoan'],
						 $temp['noidung'], $temp['ngaydang']);				
				$temp = mysql_fetch_assoc($res);
			}			
			return $obj;
		}else{
			return null;
		}
	}
	
	public function getBinhLuanByMaCH($maCH, $vaiTro, $index, $number ){
		$sql = "select * from binhluan where macauhoi='$maCH' and 
		taikhoan in (select taikhoan from nguoidung where vaitro='$vaiTro') 
		order by ngaydang DESC limit $index, $number";	
		
		return $this->extractBinhLuan($sql);
	}
	
	public function getSoLuongBLByMaCH($maCH, $vaiTro){
		$sql = "select count(*) as soluong from binhluan where macauhoi='$maCH' and 
		taikhoan in (select taikhoan from nguoidung where vaitro='$vaiTro')";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("failed getting so luong binh luan");
		}
		
		if(mysql_num_rows($res)){			
			$temp = mysql_fetch_assoc($res);
			return $temp['soluong'];
		}else{
			return null;
		}
	}
}